'use strict';

import React, { Component } from 'react';
import Immutable from 'immutable';
import Loading from './Loading';

export class BaseComponent extends Component {
    constructor(props) {
        super(props);
    }

    shouldComponentUpdate(nextProps, nextState = {}) {
        return !Immutable.is(Immutable.fromJS(this.props), Immutable.fromJS(nextProps))
            || !Immutable.is(Immutable.fromJS(this.state), Immutable.fromJS(nextState));
    }
}

export function withPageHoc(WrappedComponent) {
    return class extends Component {
        constructor(props) {
            super(props);
            this.state = {
                $loadingShow: false
            };
            this._$showLoading = this.$showLoading.bind(this);
            this._$hideLoading = this.$hideLoading.bind(this);
        }

        $showLoading() {
            this.setState({
                $loadingShow: true
            });
        }

        $hideLoading() {
            this.setState({
                $loadingShow: false
            });
        }

        render() {
            let props = {
                ...this.props,
                $showLoading: this._$showLoading,
                $hideLoading: this._$hideLoading
            };
            return this.state.$loadingShow ? <Loading/> : <WrappedComponent {...props}/>;
        }
    };
}
